<?php

include DIRETORIO."app/Models/UserTeamDAO.php";

class C_UserTeam{
	
	protected static $lk = NULL;
	
	public static function init(){
		global $db_Mysql;

		self::$lk = $db_Mysql;
	}

	public static function getDB(){
		if(self::$lk == NUll)
			self::init();

		return self::$lk;
	}

	public static function getUsersTeam($post){
		$return = null;
		
		$idTeam	= (int)$post['idTeam'];

		$objUserTeamDAO	= new UserTeamDAO(self::getDB());
		$objUsersTeam	= $objUserTeamDAO->getUsersTeam(0,$idTeam);	
		
		if(count($objUsersTeam) > 0){
			$html = '<ul>';

			foreach($objUsersTeam as $row){
				$html .= self::buildBoxUser($row);
			}

			$html .= '</ul>';
			$return[] = true;
		}else{
			$html = '<ul><li><h2>Nenhum usuário atribuido!</h2></li></ul>';
			$return[] = false;
		}
	
		$return[] = $html;	
		return json_encode($return);
	}

	public static function buildBoxUser($userTeam){

		$html	= '	<li>
						<div class="user" id="userTeam_'.$userTeam->idUser.'-'.$userTeam->idTeam.'">
							<span class="title_bar">
								<span>'.$userTeam->name.'</span>
							</span>
							<span class="title">Função: '.$userTeam->group.'<button class="expand">+</button>
							</span>
							<div class="details">
								<span>
								Login: '.$userTeam->login.'<br>
								Email: '.$userTeam->email.'<br>
								Carga Horária: '.$userTeam->hourlyLoadTeam.'<br>
								Data de Entrada: '.$userTeam->dateCreation.'<br>
								</span>
							</div>
							<div class="buttons">
								<a onclick="editUserTeam('.$userTeam->idUser.','.$userTeam->idTeam.')" href="javascript:void(0);" class="icon edit"><span>editar</span></a>
								<a onclick="deleteUserTeam('.$userTeam->idUser.','.$userTeam->idTeam.')" href="javascript:void(0);" class="icon delete"><span>deletar</span></a>								
							</div>								
						</div>
					</li>';

		
		return $html;
	}

	public static function newUserTeam($post){
		$return = null;
		
		if(count($post) > 0){
			
			$idUser = (int)$post[idUser];
			$idTeam = (int)$post[idTeam];

			$objUserTeamDAO	= new UserTeamDAO(self::getDB());
			$objUserTeam	= $objUserTeamDAO->getUsersTeam($idUser, $idTeam);

			if(count($objUserTeam) > 0){
				$return = array(false,"Este usuário já pertence a esta equipe!");
			}else{
				$objUserDAO	= new UserDAO(self::getDB());
				$objTeamDAO	= new TeamDAO(self::getDB());

				$objUser = $objUserDAO->getUsers($idUser);
				$objTeam = $objTeamDAO->getTeams($idTeam);

				$objGroups = $objUserDAO->getGroups();
				
				$selectGroups = "";
				foreach($objGroups as $row){
					if($row->idUserGroup <= 2)
						continue;
					$selectGroups .= '<option value="'.$row->idUserGroup.'">'.$row->title.'</option>';
				}

				$html = file_get_contents(TEMPLATES."userTeam.html");
				$html = str_replace("[TITLE]","Equipe / Usuário",$html);
				$html = str_replace("[ID_USER]",$idUser,$html);
				$html = str_replace("[ID_TEAM]",$idTeam,$html);
				$html = str_replace("[TEAM]",$objTeam[0]->name,$html);
				$html = str_replace("[USER]",$objUser[0]->name,$html);
				$html = str_replace("[LIST_GROUPS]",$selectGroups,$html);
				$html = str_replace("[HOURLY_LOAD_TEAM]","",$html);

				$return = array(true,$html);
			}
		}else{
			$return = array(false,"Não foi possível carregar as informações do usuário!");
		}

		return json_encode($return);
	}

	public static function editUserTeam($post){
		$return = array(false,"Não foi possível carregar as informações do usuário!");

		if(count($post) > 0){
			
			$idUser = (int)$post[idUser];
			$idTeam = (int)$post[idTeam];

			$objUserTeamDAO	= new UserTeamDAO(self::getDB());
			$objUserTeam	= $objUserTeamDAO->getUsersTeam($idUser, $idTeam);

			if(count($objUserTeam) > 0){				
				$objUserDAO	= new UserDAO(self::getDB());
				$objGroups	= $objUserDAO->getGroups();
				
				$selectGroups = "";
				foreach($objGroups as $row){
					if($row->idUserGroup <= 2)
						continue;

					if($row->idUserGroup == $objUserTeam[0]->idUserGroup){
						$selected = " selected";
					}else{
						$selected = "";
					}

					$selectGroups .= '<option value="'.$row->idUserGroup.'"'.$selected.'>'.$row->title.'</option>';
				}

				$html = file_get_contents(TEMPLATES."userTeam.html");
				$html = str_replace("[TITLE]","Equipe / Usuário",$html);
				$html = str_replace("[ID_USER]",$objUserTeam[0]->idUser,$html);
				$html = str_replace("[ID_TEAM]",$objUserTeam[0]->idTeam,$html);
				$html = str_replace("[TEAM]",$objUserTeam[0]->team,$html);
				$html = str_replace("[USER]",$objUserTeam[0]->name,$html);
				$html = str_replace("[LIST_GROUPS]",$selectGroups,$html);
				$html = str_replace("[HOURLY_LOAD_TEAM]",$objUserTeam[0]->hourlyLoadTeam,$html);

				$return = array(true,$html);
			}
		}

		return json_encode($return);
	}

	public static function saveUserTeam($post){
		$return = null;
		$id		= 0;
		
		if(count($post) > 0){
			$objUserTeamDAO	= new UserTeamDAO(self::getDB());						
			$result			= $objUserTeamDAO->save($post);
		}
		
		if($result){
			$return =  array(true,"<i>Informações do usuário salva na equipe com sucesso!");
		}else{
			$return =  array(false,"Não foi possível vincular o usuário na equipe!");
		}


		return json_encode($return);
	}


	public static function changeUserTeam($post){
		$objUserTeamDAO	= new UserTeamDAO(self::getDB());
		
		$result		= false;
		$idUser		= (int)$post[idUser];
		$idTeam		= (int)$post[idTeam];
		$idNewTeam	= (int)$post[idNewTeam];

		if($idUser > 0 && $idTeam > 0 && $idNewTeam > 0)
			$result = $objUserTeamDAO->changeUserTeam($idUser, $idTeam, $idNewTeam);

		if($result)
			$return = array(true,"Usuário movido de equipe com sucesso!");
		else
			$return = array(false,"Não foi possível mover o usuário para equipe!");


		return json_encode($return);
	}

	public static function deleteUserTeam($post){	
		$objUserTeamDAO	= new UserTeamDAO(self::getDB());

		$idUser = (int)$post[idUser];
		$idTeam = (int)$post[idTeam];

		if($idUser > 0 && $idTeam > 0)
			$result = $objUserTeamDAO->deleteUserTeam($idUser, $idTeam);

		if($result > 0)
			$return = array(true,"Usuário deletado da equipe com sucesso!");
		else
			$return = array(false,"Erro, não foi possível deletar o usuário da equipe!");

		return json_encode($return);
	}
}
?>